<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Models\Arts\WebArticleModel;
use App\Models\Arts\WebBannerModel;
use App\Models\Arts\WebVisitModel;
use App\Utils\RegionIp;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Jenssegers\Agent\Agent;

/**
 * 首页
 * Author:panda
 * Email:joeyoung0314@qq.com
 * Class ArticleController
 * @package App\Http\Controllers\Home
 */
class IndexController extends Controller
{
    public function index()
    {
        return view("welcome");
        //banner
        $view['banner'] = WebBannerModel::query()->where('status', 1)
            ->where('is_delete', 0)
            ->orderBy('sort', 'desc')
            ->orderBy('created_at', 'desc')
            ->get();
        //banner end.

        //文章列表
        $list = WebArticleModel::query()
            ->select(['id', 'menus_id', 'tags', 'title', 'image', 'is_image', 'description', 'created_at'])
            ->with(['menus' => function ($query) {
                $query->select('id', 'name');
            }])
            ->where('status', 1)
            ->where('is_delete', 0)
            ->orderBy('sort', 'desc')
            ->orderBy('created_at', 'desc')
            ->get()->toArray();
        foreach ($list as $key => $item) {
            $list[$key]['tags'] = !empty($item['tags']) ? explode(',', $item['tags']) : '';
            $list[$key]['image'] = explode(',', $item['image']);
        }
        $view['list'] = $list;
        //文章列表 end.

        // 首页精选
        $winnow = WebArticleModel::query()
            ->select(['id', 'menus_id', 'tags', 'title', 'image', 'is_image', 'description', 'created_at'])
            ->with(['menus' => function ($query) {
                $query->select('id', 'name');
            }])
            ->where('status', 1)
            ->whereRaw('FIND_IN_SET(?,tags)',['3'])
            ->where('is_delete', 0)
            ->orderBy('sort', 'desc')
            ->orderBy('created_at', 'desc')
            ->limit(2)
            ->get()->toArray();
        foreach ($winnow as $key => $item) {
            $winnow[$key]['tags'] = !empty($item['tags']) ? explode(',', $item['tags']) : '';
            $winnow[$key]['image'] = explode(',', $item['image']);
        }
        $view['winnow'] = $winnow;
        // 首页精选 end.

        //SEO
        $view['seo_title'] = '首页';
        $view['seo_keywords'] = '';
        $view['seo_description'] = '';
        return view("home.index", $view);
    }
    public function visit(Request $request)
    {
        $params    = $request->all();
        $agent     = new Agent();
        $ip        = $request->getClientIp();
        $userAgent = $params['userAgent'] ?? '';
        $res = Cache::remember("visit_{$ip}",86400,function () use($ip,$agent){
            $region    = new RegionIp();
            if (!$region->get($ip)) {
                $res = [
                    'country'  => '未知',
                    'province' => '未知',
                    'city'     => '未知',
                    'district' => '未知',
                    'ip'       => '127.0.0.1',
                ];
            } else {
                $res = $region->getData();
            }
            return $res;
        });
        // 省市区
        $insert['country']  = $res['country'];
        $insert['province'] = $res['province'];
        $insert['city']     = $res['city'];
        $insert['district'] = $res['district'];

        $insert['url']         = $params['url'] ?? '';
        $insert['ip']          = $ip;
        $insert['session_id']  = $params['sessionId'] ?? '';
        $insert['platform']    = $agent->platform($userAgent);
        $insert['referrer']    = !empty($params['referrer']) ? $params['referrer'] : '直接访问';
        $insert['browser']     = $agent->browser($userAgent);
        $insert['user_agent']  = $userAgent;
        $insert['language']    = $params['language'] ?? '';
        $insert['is_robot']    = $agent->isRobot($userAgent);
        $insert['create_hour'] = date('H');
        $insert['create_date'] = date('Y-m-d');
        $insert['created_at']  = time();
        WebVisitModel::query()->insert($insert);
    }
}